package com.example.szys.controler;

import com.example.szys.POJO.HttpResult;
import com.example.szys.POJO.Review;
import com.example.szys.model.MistakeBook;
import com.example.szys.model.WareHouse;
import com.example.szys.repository.MistakeBookRepository;
import com.example.szys.repository.WareHouseRepository;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class MistakeBookControler {
    @Autowired
    private MistakeBookRepository mistakeBookRepository;

    @Autowired
    private WareHouseRepository wareHouseRepository;

    @PostMapping("/savemistakebook")
    public HttpResult<Integer> saveMistakeBook(@RequestParam("email") String email,@RequestParam("warehouseid") int warehouseid,@RequestParam("myanswer") String myanswer){
        HttpResult<Integer> integerHttpResult=new HttpResult<>();
        if (mistakeBookRepository.findByEmailAndWareId(email,warehouseid)==null) {
            MistakeBook mistakeBook = new MistakeBook();
            mistakeBook.setEmail(email);
            mistakeBook.setWareId(warehouseid);
            mistakeBook.setMyAnswer(myanswer);
            mistakeBookRepository.save(mistakeBook);
            integerHttpResult.setResult(1);
        }else {
            integerHttpResult.setResult(0);
        }
        return integerHttpResult;
    }

    @PostMapping("/savemistakebooks")
    public HttpResult<Integer> saveMistakeBooks(@RequestParam("email") String email,@RequestParam("list") String list,@RequestParam("myanswer") String myanswer){
        HttpResult<Integer> integerHttpResult=new HttpResult<>();
        Gson gson=new Gson();
        ArrayList<Integer> id=gson.fromJson(list,new TypeToken<List <Integer>>(){}.getType());
        ArrayList<String> my=gson.fromJson(myanswer,new TypeToken<List<String>>(){}.getType());
        int count=0;
        for (int i=0;i<id.size();i++){
            if (mistakeBookRepository.findByEmailAndWareId(email,id.get(i))==null) {
                MistakeBook m = new MistakeBook();
                m.setEmail(email);
                m.setWareId(id.get(i));
                m.setMyAnswer(my.get(i));
                mistakeBookRepository.save(m);
                count++;
            }
        }
        integerHttpResult.setContent(count);
        integerHttpResult.setResult(1);
        return integerHttpResult;
    }

    @PostMapping("/getmistakebooks")
    public HttpResult<ArrayList<Review>> getmistakebooks(@RequestParam("email") String email){
        HttpResult<ArrayList<Review>> httpResult=new HttpResult<>();
        List<MistakeBook> list=mistakeBookRepository.findByEmail(email);
        ArrayList<Review> reviews=new ArrayList<>();
        for (MistakeBook m:list){
            WareHouse w=wareHouseRepository.findOne(m.getWareId());
            if (w!=null) {
                Review r = new Review();
                r.setQuestion(w.getQuestion());
                r.setAnswer(w.getAnswer());
                r.setId(w.getId());
                r.setMyAnswer(m.getMyAnswer());
                r.setCollect(1);
                reviews.add(r);
            }
        }
        httpResult.setContent(reviews);
        return httpResult;
    }
}
